package com.ruoyi.quartz.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.WeatherForecast;
import com.ruoyi.system.service.IWeatherForecastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("weatherTask")
public class WeatherTask {

    private static final String WEATHER_URL = "http://www.nmc.cn/rest/weather?stationid=lZXKF&_=1742117787210";
    @Autowired
    private IWeatherForecastService weatherForecastService;

    /**
     * 抓取天气信息
     */
    public void getWeatherInfo() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            String result = HttpUtil.get(WEATHER_URL);
            System.out.println("result:"+result);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject dataJsonObj = jsonObject.getJSONObject("data");
            //获取到省份和城市
            JSONObject stationObj = dataJsonObj.getJSONObject("real").getJSONObject("station");
            String province = stationObj.getString("province");
            String city = stationObj.getString("city");
            //获取未来七天的预报天气
            JSONObject predictObj = dataJsonObj.getJSONObject("predict");
            JSONArray detail = predictObj.getJSONArray("detail");
            detail.forEach(item -> {
                WeatherForecast weatherForecast = new WeatherForecast();
                JSONObject jsonObject1 = (JSONObject) item;
                JSONObject day = jsonObject1.getJSONObject("day");
                JSONObject weather = day.getJSONObject("weather");
                String info = weather.getString("info");
                Integer temperature1 = weather.getInteger("temperature");

                JSONObject wind = day.getJSONObject("wind");
                String direct = wind.getString("direct");
                String power = wind.getString("power");
                JSONObject night = jsonObject1.getJSONObject("night");
                Integer temperature2 = night.getJSONObject("weather").getInteger("temperature");
                if (temperature1 == 9999){
                    temperature1 = temperature2;
                    weatherForecast.setDayWeatherTemperature(Long.valueOf(temperature1));
                }else{
                    weatherForecast.setDayWeatherTemperature(Long.valueOf(temperature1));
                }
                String date = jsonObject1.getString("date");
                weatherForecast.setProvince(province);
                weatherForecast.setCity(city);
                weatherForecast.setWeatherInfo(info);

                weatherForecast.setNightWeatherTemperature(Long.valueOf(temperature2));
                weatherForecast.setWindDirect(direct);
                weatherForecast.setWindPower(power);
                WeatherForecast ent =weatherForecastService.selectWeatherForecastByDate(date);
                if (ent!=null){
                    weatherForecast.setId(ent.getId());
                    weatherForecastService.updateWeatherForecast(weatherForecast);
                    System.out.println("成功更新" + date + "的数据");
                }else{
                    try {
                        Date parse = simpleDateFormat.parse(date);
                        weatherForecast.setDate(parse);
                        weatherForecastService.insertWeatherForecast(weatherForecast);
                        System.out.println("成功插入" + date + "的数据");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
